/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;


import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class NormalJmsPanel extends Content {

	private TextBox jmsFac, serverUrl, destUrl;
	
	private ServiceMessages cons;
	
	private NormalJms info ;
	
	public NormalJmsPanel(ServiceMessages scons, NormalJms info1) {
		this.cons = scons;
		this.info = info1;
		
		VerticalPanel panel = new VerticalPanel();
		
		panel.setWidth("100%");
		
		Grid jms = new Grid(3, 2);
		jms.setWidth("80%");
		jms.setCellPadding(1);
		jms.setCellSpacing(1);
		jms.setBorderWidth(1);

		ColumnFormatter cf = jms.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "55%");

		CellFormatter ff = jms.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		
		jms.setHTML(0, 0, cons.jmsServerUrl());
		jms.setHTML(1, 0, cons.jmsFac());
		jms.setHTML(2, 0, cons.jmsDest());
		
		serverUrl = new TextBox();
		serverUrl.setVisibleLength(64);

		jmsFac = new TextBox();
		jmsFac.setVisibleLength(40);

		destUrl = new TextBox();
		destUrl.setVisibleLength(40);
		
		jms.setWidget(0, 1, serverUrl);
		jms.setWidget(1, 1, jmsFac);
		jms.setWidget(2, 1, destUrl);
		
		if(info != null){
			serverUrl.setText(info.getServerUrl());
			jmsFac.setText(info.getFactoryUrl());
			destUrl.setText(info.getDestinationJndi());
		}
		
		panel.add(jms);
		
		initWidget(panel);

	}
	
	public NormalJms getNormalJms(){
		if (!StringUtils.checkLenght(serverUrl.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.jmsServerUrl(), "1", "1024"));
			return null;
		}
		if (!StringUtils.checkLenght(jmsFac.getText(), 1, 512)) {
			Window.alert(cons.lengthError(cons.jmsServerUrl(), "1", "512"));
			return null;
		}
		if (!StringUtils.checkLenght(destUrl.getText(), 1, 512)) {
			Window.alert(cons.lengthError(cons.jmsDest(), "1", "512"));
			return null;
		}
		if(info == null)
			info = new NormalJms();
		
		info.setDestinationJndi(destUrl.getText());
		info.setFactoryUrl(jmsFac.getText());
		info.setServerUrl(serverUrl.getText());
		return info;
	}

}
